# coding: utf-8
#
import uiautomator2 as u2
import time
import os

#d = u2.connect_wifi('192.168.0.106:5003')

dvice='192.168.0.106:5002'

d = u2.connect(dvice)

d.app_start('com.zhixuan.ancientpoetry')

d.implicitly_wait(10)

time.sleep(2)

try:
    if(d(resourceId='com.innke.zhanshang:id/tv_content').exists):
        print('first')
except:
    print("except ............")
    print(os.popen("adb -s "+dvice+ " shell /data/local/tmp/atx-agent server -d --stop").read())
    time.sleep(8)

    d = u2.connect(dvice)
    d.app_start('com.innke.zhanshang')


if(d(resourceId='com.innke.zhanshang:id/tv_content').exists):
    print('first')
    intimity=d(resourceId='com.innke.zhanshang:id/tv_content').get_text();
    print(d(resourceId='com.innke.zhanshang:id/tv_content').get_text());

    str1='我们尊重并保护所有用户的个人隐私权，为了给您提供更准确，更有个性化对服务，制选App会按照隐私政策的规定使用和披露您的个人信息'
    str2='《隐私政策》查看路径：我的-设置-隐私政策，如您同意以上协议内容，请点击“同意”，开始使用我们的产品和服务'

    '''if(str1 in intimity or str2 in intimity):
        print('intimity true')
        d.click(0.448, 0.595)
        time.sleep(1)
        d.swipe_ext("up")
        d.press('back')

        d.click(0.673, 0.582)
        d.swipe_ext("up")
        d.press('back')'''

    d.click(0.564, 0.044)
    d.click(0.065, 0.044)

    if(d(resourceId='com.innke.zhanshang:id/btn_agree').exists):
        d(resourceId='com.innke.zhanshang:id/btn_agree').click()
        time.sleep(1)

    d.click(0.564, 0.044)
    d.click(0.065, 0.044)

if(d(resourceId='com.innke.zhanshang:id/adsFl').exists):
    d(resourceId='com.innke.zhanshang:id/adsFl').click()


if(d(resourceId='com.android.permissioncontroller:id/permission_message').exists):
    d(resourceId='com.android.permissioncontroller:id/permission_deny_button_two').click()


while(d(resourceId='com.innke.zhanshang:id/vp_guide_pager').exists):
    print('vp_guide_pager true')
    d.swipe_ext('left')
    if(d(resourceId='com.innke.zhanshang:id/btn_guide_complete').exists):
        d(resourceId='com.innke.zhanshang:id/btn_guide_complete').click()

if(d(resourceId='android:id/parentPanel').exists):
    print('parentPanel true')
    d.click(0.495, 0.743)

if(d(resourceId='com.innke.zhanshang:id/welcomeTop').exists):
    print('welcomeTop true')
    time.sleep(5)
    parentPanel=True
    i=0
    while(parentPanel):
        if(d(resourceId='android:id/parentPanel').exists):
            print('parentPanel true ',i)
            d.click(0.495, 0.743)
            time.sleep(1)
        else:
            parentPanel=False
        i+=1


f=True
i=0
while(f):
    if(d(resourceId='com.innke.zhanshang:id/ll_jcsb').exists):
        f=False
        print('ll_jcsb true ',i)
        d.swipe_ext("up")
        if(i%5):
            if(d(resourceId='android:id/parentPanel').exists):
                print('parentPanel true 22 ')
                d.click(0.495, 0.743)
    else:
        print('ll_jcsb false sleep')
        time.sleep(1)
    i+=1
    if(i>10):
        break

'''
if d(resourceId='com.example.myapplication:id/home_uapp').exists:
    d(resourceId="com.example.myapplication:id/home_uapp").click()
else:
    d.press('back')
    time.sleep(3)
    if d(resourceId='com.example.myapplication:id/home_uapp').exists:
        d(resourceId="com.example.myapplication:id/home_uapp").click()


d(resourceId="com.example.myapplication:id/analytics_g1_b1").click()

d.swipe_ext("up")

d(resourceId="com.example.myapplication:id/analytics_g5_b1").click()

d.swipe_ext("down")
'''
d.press('back')
d.press('back')